package cn.xueden.common.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

/**功能描述：缓存缓存命中监控切面
 * @author:梁志杰
 * @date:2025/6/8
 * @description:cn.xueden.common.aop
 * @version:1.0
 */
@Aspect
@Component
@Slf4j
public class CacheMonitorAspect {

    @Around("@annotation(org.springframework.cache.annotation.Cacheable)")
    public Object logCacheHit(ProceedingJoinPoint joinPoint) throws Throwable {
        Object result = joinPoint.proceed();
        String cacheName = ((Cacheable) ((MethodSignature) joinPoint.getSignature())
                .getMethod().getAnnotation(Cacheable.class)).value()[0];
        log.info("Cache hit for: " + cacheName);
        return result;
    }
}
